package com.farmproduct.service;

import com.farmproduct.dao.SellUserMapper;
import com.farmproduct.pojo.SellUser;
import com.farmproduct.pojo.SellUserExample;
import com.farmproduct.vo.LoginRequestVO;
import com.farmproduct.vo.PasswordForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SellUserService {
    @Autowired
    private SellUserMapper sellUserMapper;
    @Autowired
    private SellUserExample sellUserExample;

    /**
     * 登陆
    **/
    public SellUser login(LoginRequestVO loginRequestVO){
        sellUserExample.clear();
        sellUserExample.createCriteria().andUsernameEqualTo(loginRequestVO.getUsername()).andPasswordEqualTo(loginRequestVO.getPassword());
        List<SellUser> sellUsers = sellUserMapper.selectByExample(sellUserExample);
        if (sellUsers!=null){
            SellUser sellUser = sellUsers.get(0);
            sellUser.setPassword("");
            return sellUser;
        }
        return null;
    }

    /**
     * 添加用户
     * */
    public boolean addSellUser(SellUser sellUser){
        sellUserExample.clear();
        if (sellUserMapper.insert(sellUser)==1){
            return true;
        }
        return false;
    }

    /**
     * 删除用户
     * */
    public boolean delSellUser(SellUser sellUser){
        sellUserExample.clear();
        if (sellUserMapper.deleteByPrimaryKey(sellUser.getUid())==1){
            return true;
        }
        return false;
    }

    /**
     * 更新用户
     * */
    public boolean updateSellUser(SellUser sellUser){
        sellUserExample.clear();
        sellUserExample.createCriteria().andUidEqualTo(sellUser.getUid());
        List<SellUser> sellUsers = sellUserMapper.selectByExample(sellUserExample);
        if (sellUsers!=null){
            SellUser sellUser1 = sellUsers.get(0);
            sellUser.setPassword(sellUser1.getPassword());
            if (sellUserMapper.updateByPrimaryKey(sellUser)==1){
                return true;
            }
        }
        return false;
    }
    public boolean updateSellUserPassword(PasswordForm passwordForm){
        sellUserExample.clear();
        sellUserExample.createCriteria().andUidEqualTo(passwordForm.getUid()).andPasswordEqualTo(passwordForm.getOldpassword());
        List<SellUser> sellUsers = sellUserMapper.selectByExample(sellUserExample);
        if (sellUsers!=null){
            SellUser sellUser1 = sellUsers.get(0);
            sellUser1.setPassword(passwordForm.getNewpassword());
            if (sellUserMapper.updateByPrimaryKey(sellUser1)==1){
                return true;
            }
        }
        return false;
    }


    public SellUser getUserById(String uid){
        sellUserExample.clear();
        sellUserExample.createCriteria().andUidEqualTo(uid);
        List<SellUser> sellUsers = sellUserMapper.selectByExample(sellUserExample);
        if(sellUsers!=null&&sellUsers.size()>0){
            SellUser sellUser = sellUsers.get(0);
            return sellUser;
        }
        return null;
    }

    public long getUserConunt(){
        sellUserExample.clear();
        return sellUserMapper.countByExample(sellUserExample);
    }
}
